<?php
/**
 * 广告位
 * User: yuanxuncheng
 * Date: 2016-12-21
 * Time: 22:39
 */

namespace app\api\model\dao;

use app\library\Flag;
use think\Model;
use think\Db;

class AdPosition extends Model
{
    //set talbename
    protected $table = 'ad_position';
    //set primary key
    protected $pk = 'position_id';
    //自动写入时间戳
    protected $autoWriteTimestamp = true;

    /**
     * @desc 添加广告位
     * @param $param
     * @return false|int
     */
    public function addAdPosition($param)
    {
        $this->allowField(true)->save($param);
        return $this->data[$this->pk];
    }

    public function updateAdPosition($position_id, $param)
    {
        $conds = array(
            'position_id' => $position_id,
        );
        $field = array();
        if (isset($param['position_name'])) {
            $field['position_name'] = $param['position_name'];
        }

        if (isset($param['ad_width'])) {
            $field['ad_width'] = $param['ad_width'];
        }

        if (isset($param['ad_height'])) {
            $field['ad_height'] = $param['ad_height'];
        }

        if (isset($param['position_desc'])) {
            $field['position_desc'] = $param['position_desc'];
        }

        if (isset($param['position_style'])) {
            $field['position_style'] = $param['position_style'];
        }

        if (isset($param['is_open'])) {
            $field['is_open'] = $param['is_open'];
        }

        if (isset($param['status'])) {
            $field['status'] = $param['status'];
        }

        return $this->isUpdate(true)->allowField(true)->save($field, $conds);
    }

    /**
     * @param $id
     * @return array|false|\PDOStatement|string|Model
     */
    public function getAdPositionById($id)
    {
        $conds = [
            'position_id' => $id
        ];
        return $this->where($conds)->find();
    }

    /**
     * @return \think\paginator\Collection
     */
    public function getAdPosition()
    {
        return $this->where(['status' => '0'])->paginate(10);
    }
}
